All Questions
9 questions
3votes
1answer
319views
Efficiently calculate value of Pascal's Triangle using memoization and recursion
So reading about functional programming, applications to dynamic programming, and learning Scala. I figured I would try it out with a popular example, Pascal's Triangle, tests against known values ...
2votes
0answers
187views
generic implementation approaches for a recursive function
Let's take the famous fibonacci problem as an example but this is a generic problem. Also, taking scala as the language as it's rather feature-rich. I want to know which solution you'd prefer. we all ...
1vote
1answer
121views
Find first repeating Char in String
Given a string, find the first repeating character in it. Examples: firstUnique("Vikrant") → None ...
4votes
2answers
450views
Replace array element with multiplication of neighbors in Scala
Given an array of integers, update the index with multiplication of previous and next integers, Input: 2 , 3, 4, 5, 6 Output: 2*3, 2*4, 3*5, 4*6, 5*6 Following ...
0votes
1answer
2kviews
Cartesian product in Scala
I'm using this code to compute a cartesian product: ...
2votes
1answer
105views
Selecting subset of size k using recursion [closed]
Sometimes I need to implement recursive algorithms that pass a certain state from one recursive call to another. For example, a greedy subset selection: we have a set of candidate objects, and we ...
2votes
3answers
1kviews
Tail-Recursion to get a Map of word counts
I want to read a file and store the number of occurrences of each word in a Map, using tail recursion. I came up with the following; it seems to work; does it look like it's right? ...
3votes
3answers
131views
Removing nested blocks from a string
I wrote this function in scala that uses tail recursion to remove nested blocks from a text. Usage examples: ...
5votes
1answer
3kviews
Tail-recursive factorial
Is there anything what could be improved on this code? def factorial(n: Int, offset: Int = 1): Int = { if(n == 0) offset else factorial(n - 1, (offset * n)) } ...